package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import server.PutCache;

import model.Domain;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

import data.Data;

@SuppressWarnings("serial")
public class PostDataServlet extends HttpServlet {
	@SuppressWarnings({ "unchecked", "static-access" })
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		String name = req.getParameter("name");
		String wotTrust = req.getParameter("wotTrust");
		String wotVendor = req.getParameter("wotVendor");
		String wotPrivacy = req.getParameter("wotPrivacy");
		String wotChild = req.getParameter("wotChild");
		String alexa = req.getParameter("alexa");
		String google = req.getParameter("google");
		String ip = req.getParameter("ip");
		String speedSite = req.getParameter("speedSite");
		String ageDomain = req.getParameter("ageDomain");
		String charset = req.getParameter("charset");
		String language = req.getParameter("language");
		String server = req.getParameter("server");
		String jsLib = req.getParameter("jsLib");
		String outlink = req.getParameter("outlink");
		String whois = req.getParameter("whois");
		String whoisIp = req.getParameter("whoisIp");
		String title = req.getParameter("title");
		String description = req.getParameter("description");
		String keyword = req.getParameter("keyword");
		String domain = req.getParameter("domain");
		String adsend = req.getParameter("adsend");
		String analytics = req.getParameter("analytics");
		String visit = req.getParameter("visit");
		alexa = alexa.replaceAll("[^0-9]+", "");
		if(alexa.equals(""))
		{
			alexa="0";
		}
	
		PersistenceManager psm = Data.get().getPersistenceManager();
		Key k = KeyFactory.createKey(Domain.class.getSimpleName(), name);
	    Domain domain1;
        try {
        	domain1 = psm.getObjectById(Domain.class, k);
		} catch (JDOObjectNotFoundException e) {
			int ran = new Random().nextInt(1000000);
        	if(!alexa.equals("0"))
        	{
        		ran = Integer.parseInt(alexa);
        	}
        	Query query = psm.newQuery(Domain.class);
        	query.setFilter("alexa < "+ran);
        	query.setOrdering("alexa desc");
        	query.setRange(0, 30);

        	List<Domain> domainList=  (List<Domain>)query.execute();
        	ArrayList<String> nearSite = new ArrayList<String> ();
        	if(domainList.size() > 0)
        	{
        		for (int i = 0; i < domainList.size(); i++) {
        			nearSite.add(domainList.get(i).getName());
				}
        	}
			
			domain1 = new Domain();
			domain1.setNearSite(nearSite);
			domain1.setName(name);
			domain1.setWotTrust(new Text(wotTrust));
			domain1.setWotVendor(new Text(wotVendor));
			domain1.setWotPrivacy(new Text(wotPrivacy));
			domain1.setWotChild(new Text(wotChild));
			domain1.setAlexa(Integer.valueOf(alexa));
			domain1.setGoogle(new Text(google));
			domain1.setIp(ip);
			domain1.setSpeedSite(new Text(speedSite));
			domain1.setAgeDomain(new Text(ageDomain));
			domain1.setCharset(new Text(charset));
			domain1.setLanguage(new Text(language));
			domain1.setServer(new Text(server));
			domain1.setJsLib(new Text(jsLib));
			domain1.setOutlink(new Text(outlink));
			domain1.setWhois(new Text(whois));
			domain1.setWhoisIp(new Text(whoisIp));
			domain1.setTitle(new Text(title));
			domain1.setDescription(new Text(description));
			domain1.setKeyword(new Text(keyword));
			domain1.setDomain(domain);
			domain1.setAdsend(adsend);
			domain1.setAnalytics(analytics);
			domain1.setVisit(new Text(visit));
			psm.makePersistent(domain1);
			
			PutCache putCache = new PutCache();
			putCache.putCacheDomain(domain1,"lastupdates");
		}
	}
}
